﻿/***************************************************************************
 * 文件名：MainForm
 * 功能：
 * 说明： 头文件注释主窗体
 * -------------------------------------------------------------------------
 * 创建时间：2025/7/10 19:50:43
 * 创建人：葛浩
 * 邮箱： 2094733071@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Util;
using OpenFileDialog = System.Windows.Forms.OpenFileDialog;

namespace BuiltInTools
{
	public partial class HeaderFileMainForm : XtraForm
	{
		#region Propertys

		/// <summary>
		/// 当前用户的AppData路径
		/// </summary>
		private string _LocalDirectory = string.Empty;

		private List<FilePathModel> FilePathModels = new List<FilePathModel>();

		#endregion

		#region Const

		/// <summary>
		/// 文件存储文件夹名称
		/// </summary>
		private const string c_AuxiliaryTools = "AuxiliaryTools";

		/// <summary>
		/// 头文件注释模板所在文件夹名称
		/// </summary>
		private const string c_HeaderFileCommentTemplate = "HeaderFileCommentTemplate";

		/// <summary>
		///  头文件模板文件
		/// </summary>
		private const string c_Template = "Template.txt";

		/// <summary>
		/// 自定义头文件模板
		/// </summary>
		private const string c_CustomTemplate = "CustomTemplate.txt";

		/// <summary>
		/// 头文件路径配置文件
		/// </summary>
		private const string c_HeaderFilePathConfiguration = "HeaderFilePathConfiguration.txt";

		/// <summary>
		/// 头文件注释内容
		/// </summary>
		private const string c_TemplateFileContext = "/***************************************************************************\r\n " +
													 "* 文件名：$safeitemname$\r\n " +
													 "* 功能：\r\n " +
													 "* 说明：\r\n " +
													 "* -------------------------------------------------------------------------\r\n " +
													 "* 创建时间：$time$\r\n " +
													 "* 创建人：××\r\n " +
													 "* 邮箱： ×××××××××××@qq.com\r\n " +
													 "* =========================================================================\r\n " +
													 "*\r\n " +
													 "* 修改人：   \r\n " +
													 "* 修改时间：   \r\n " +
													 "* 修改说明：   \r\n " +
													 "***************************************************************************/";

		/// <summary>
		/// 一般类的路径
		/// </summary>
		private string c_GeneralClass = "ItemTemplates\\CSharp\\Code\\2052\\Class\\Class.cs";
		/// <summary>
		/// 接口类的路径
		/// </summary>
		private string c_InterfaceClass = "ItemTemplates\\CSharp\\Code\\2052\\Interface\\Interface.cs";
		/// <summary>
		/// Web项目的一般类
		/// </summary>
		private string c_WebClass = "ItemTemplates\\CSharp\\Code\\2052\\WebClass\\Class.cs";
		/// <summary>
		/// 用户控件类
		/// </summary>
		private string c_UserControl = "ItemTemplates\\CSharp\\Windows Forms\\2052\\UserControl\\usercontrol.cs";
		/// <summary>
		/// Form类
		/// </summary>
		private string c_Form = "ItemTemplates\\CSharp\\Windows Forms\\2052\\Form\\form.cs";

		/// <summary>
		/// 模板备份文件夹名称
		/// </summary>
		private const string c_BackupTemplateName = "BackUpTemplate";

		#endregion

		#region Win

		public HeaderFileMainForm()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Load事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void MainForm_Load(object sender, System.EventArgs e)
		{
			InitFileTemplateList();
			InitHeaderFileConfiguration();
			GetVsInstallPath();
		}

		#endregion

		#region Methods

		/// <summary>
		/// 初始化头文件注释模板
		/// 检测是否存在文件模板，不存在生成至程序目录中
		/// </summary>
		private void InitFileTemplateList()
		{
			try
			{
				_LocalDirectory = FileHelperUtil.GetAppDataFolderPath();
				if (string.IsNullOrEmpty(_LocalDirectory))
				{
					return;
				}

				/// 检测模板文件夹是否存在
				string headerFileCommentTemplateDir = Path.Combine(_LocalDirectory, c_AuxiliaryTools, c_HeaderFileCommentTemplate);
				string headerFileCommentTemplatePath = Path.Combine(headerFileCommentTemplateDir, c_Template);
				//若模板文件不存在，则创建模板文件
				if (!File.Exists(headerFileCommentTemplatePath))
				{
					//检测模板文件夹是否存在，不存在则创建模板文件夹
					if (!Directory.Exists(headerFileCommentTemplateDir))
					{
						Directory.CreateDirectory(headerFileCommentTemplateDir);
					}

					//写入文件
					using (FileStream fs = new FileStream(headerFileCommentTemplatePath, FileMode.Create))
					{
						using (StreamWriter writer = new StreamWriter(fs))
						{
							writer.Write(c_TemplateFileContext);
						}
					}
				}

				//检测是否存在自定义模板文件
				string customTemplatePath = Path.Combine(headerFileCommentTemplateDir, c_CustomTemplate);
				if (!File.Exists(customTemplatePath))
				{
					txtHeaderFileContext.Text = c_TemplateFileContext;
				}
				else
				{
					//读取自定义模板文件内容
					using (StreamReader sr = new StreamReader(customTemplatePath))
					{
						txtHeaderFileContext.Text = sr.ReadToEnd();
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 初始化头文件路径配置
		/// </summary>
		private void InitHeaderFileConfiguration()
		{
			try
			{
				FilePathModels.Clear();

				//读取默认的头文件注释模板
				string headerFileConfigurationPath = Path.Combine(_LocalDirectory, c_AuxiliaryTools, c_HeaderFileCommentTemplate, c_HeaderFilePathConfiguration);
				if (!File.Exists(headerFileConfigurationPath))
				{
					FilePathModels.Add(new FilePathModel { FilePath = c_GeneralClass });
					FilePathModels.Add(new FilePathModel { FilePath = c_InterfaceClass });
					FilePathModels.Add(new FilePathModel { FilePath = c_WebClass });
					FilePathModels.Add(new FilePathModel { FilePath = c_UserControl });
					FilePathModels.Add(new FilePathModel { FilePath = c_Form });

					//若不存在则创建默认的头文件路径配置文件
					using (FileStream fs = new FileStream(headerFileConfigurationPath, FileMode.Create))
					{
						using (StreamWriter writer = new StreamWriter(fs))
						{
							foreach (var path in FilePathModels)
							{
								writer.WriteLine(path.FilePath);
							}
						}
					}
				}
				else
				{
					//读取头文件配置路径
					using (StreamReader sr = new StreamReader(headerFileConfigurationPath))
					{
						while (!sr.EndOfStream)
						{
							string path = sr.ReadLine();
							if (!string.IsNullOrEmpty(path))
							{
								FilePathModels.Add(new FilePathModel { FilePath = path });
							}
						}
					}
				}

				bsData.DataSource = FilePathModels;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 获取vs安装路径
		/// </summary>
		/// <returns></returns>
		private void GetVsInstallPath()
		{
			try
			{
				string strKeyName = string.Empty;
				string softPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\";
				RegistryKey regKey = Registry.LocalMachine;
				RegistryKey regSubKey = regKey.OpenSubKey(softPath + "devenv" + ".exe", false);

				object objResult = regSubKey.GetValue(strKeyName);
				RegistryValueKind regValueKind = regSubKey.GetValueKind(strKeyName);
				string visualStudioTemplatePath = string.Empty;
				if (regValueKind == RegistryValueKind.String)
				{
					visualStudioTemplatePath = objResult.ToString();
				}

				if (string.IsNullOrEmpty(visualStudioTemplatePath))
				{
					return;
				}

				txtVisualStudioTemplatePath.Text = visualStudioTemplatePath.Replace("devenv.exe", "").Replace("\"", "");

			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 备份模板文件
		/// </summary>
		/// <param name="backUpTemplateFilePath">要备份的文件路径</param>
		private void BackUpTemplateFile(string backUpTemplateFilePath)
		{
			try
			{
				//检测备份文件路径是否为空或文件不存在
				if (string.IsNullOrEmpty(backUpTemplateFilePath))
				{
					MessageBox.Show("要备份文件的路径不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				if (!File.Exists(backUpTemplateFilePath))
				{
					MessageBox.Show("要备份的文件不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				string backupDirectory = Path.Combine(_LocalDirectory, c_AuxiliaryTools, c_HeaderFileCommentTemplate, c_BackupTemplateName);
				if (!Directory.Exists(backupDirectory))
				{
					Directory.CreateDirectory(backupDirectory);
				}

				string searchString = "IDE\\";
				int index = backUpTemplateFilePath.IndexOf(searchString);
				if (index == -1)
				{
					searchString = "ide\\";
					index = backUpTemplateFilePath.IndexOf(searchString);
					if (index == -1)
					{
						MessageBox.Show("请选择Visual Studio安装目录下的模板文件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
						return;
					}
				}

				//截取安装目录后的路径
				int startIndex = index + searchString.Length;
				string path = backUpTemplateFilePath.Substring(startIndex, backUpTemplateFilePath.Length - startIndex - 3);
				List<string> files = path.Split('\\').ToList();
				if (files?.Count == 0)
				{
					return;
				}

				//构建备份文件名称，便于还原文件使用
				string backupFileName = string.Empty;
				int i = 1;
				foreach (string info in files)
				{
					backupFileName += info + (i < files.Count ? "_" : "");
					i++;
				}

				//备份文件路径
				string backupFilePath = Path.Combine(backupDirectory, backupFileName + ".cs");

				//检测备份文件是否存在，若存在则不备份文件
				if (File.Exists(backupFilePath))
				{
					return;
				}

				//拷贝文件到备份目录
				File.Copy(backUpTemplateFilePath, backupFilePath);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 添加到配置中
		/// </summary>
		/// <param name="filePath"></param>
		private void AddTemplateConfiguration(string filePath)
		{
			if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
			{
				return;
			}

			string searchString = "IDE\\";
			int index = filePath.IndexOf(searchString);
			if (index == -1)
			{
				searchString = "ide\\";
				index = filePath.IndexOf(searchString);
				if (index == -1)
				{
					return;
				}
			}

			int startIndex = index + searchString.Length;
			string path = filePath.Substring(startIndex, filePath.Length - startIndex);

			FilePathModels.Add(new FilePathModel { FilePath = path });

			//读取默认的头文件注释模板
			string headerFileConfigurationPath = Path.Combine(_LocalDirectory, c_AuxiliaryTools, c_HeaderFileCommentTemplate, c_HeaderFilePathConfiguration);

			using (StreamWriter writer = new StreamWriter(headerFileConfigurationPath))
			{
				writer.Write("");
				foreach (var item in FilePathModels)
				{
					writer.WriteLine(item.FilePath);
				}
			}

			///刷新数据源
			bsData.DataSource = FilePathModels;
			gridControl1.Views[0].RefreshData();
		}

		/// <summary>
		/// 替换VS中部分文件的头文件信息
		/// </summary>
		private void ReplaceVisualHeaderFile(string filePath = "")
		{
			try
			{
				#region 验证文件头注释模板是否为空

				string visualStudioTemplatePath = txtVisualStudioTemplatePath.Text.Trim();
				string headerFileContext = txtHeaderFileContext.Text;

				if (string.IsNullOrEmpty(headerFileContext))
				{
					MessageBox.Show("请输入文件头注释内容！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				#endregion

				#region 替换文件头

				if (string.IsNullOrEmpty(visualStudioTemplatePath))
				{
					MessageBox.Show("Visual Studio 程序未安装！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				if (!Directory.Exists(visualStudioTemplatePath))
				{
					MessageBox.Show("Visual Studio 程序模板文件文件夹不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				//要替换的文件路径列表
				List<string> filePaths = new List<string>();

				//检查文件路径是否为空,若不为空则比较当前路径是否以程序所在路径开始
				if (!string.IsNullOrEmpty(filePath))
				{
					filePaths.Add(filePath);
				}
				else
				{
					foreach (var path in FilePathModels)
					{
						filePaths.Add(Path.Combine(visualStudioTemplatePath, path.FilePath));
					}
				}

				if (filePaths?.Count == 0)
				{
					return;
				}

				#endregion

				#region 添加头文件注释

				//遍历执行文件路径列表，添加头文件注释
				foreach (var path in filePaths)
				{
					if (string.IsNullOrEmpty(path) || !File.Exists(path))
					{
						continue;
					}

					//临时文件路径
					string tempCsFilePath = path + ".tmp";

					//检测当前文件是否有写入权限
					if (!FileHelperUtil.ReadPathRule(path))
					{
						bool isAddSecuritySucess = FileHelperUtil.AddSecurityControllFile(tempCsFilePath);
						if (!isAddSecuritySucess)
						{
							return;
						}
					}

					// 读取要添加头文件注释的文件内容
					string targetCsContent = File.ReadAllText(path);
					if (string.IsNullOrEmpty(targetCsContent))
					{
						continue;
					}

					// 头文件注释的内容写入临时文件
					File.WriteAllText(tempCsFilePath, headerFileContext + Environment.NewLine + targetCsContent);

					//删除之前先备份文件
					BackUpTemplateFile(path);

					// 删除原文件
					File.Delete(path);

					// 重命名临时文件
					File.Move(tempCsFilePath, path);
				}

				MessageBox.Show("文件执行成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

				#endregion
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		#endregion

		#region Event

		/// <summary>
		/// 保存模板 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnSaveTemplate_Click(object sender, EventArgs e)
		{
			try
			{
				string headerFileContext = txtHeaderFileContext.Text.Trim();
				if (string.IsNullOrEmpty(headerFileContext))
				{
					MessageBox.Show("请先输入头文件注释内容！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				if (string.IsNullOrEmpty(_LocalDirectory))
				{
					_LocalDirectory = FileHelperUtil.GetAppDataFolderPath();
				}
				string customTemplatePath = Path.Combine(_LocalDirectory, c_AuxiliaryTools, c_HeaderFileCommentTemplate, c_CustomTemplate);

				#region 生成自定义头文件注释模板

				//检测是否存在自定义头文件模板
				if (File.Exists(customTemplatePath))
				{
					File.WriteAllText(customTemplatePath, headerFileContext);
				}
				else
				{
					//写入文件
					using (FileStream fs = new FileStream(customTemplatePath, FileMode.Create))
					{
						using (StreamWriter writer = new StreamWriter(fs))
						{
							writer.Write(headerFileContext);
						}
					}
				}

				#endregion

				MessageBox.Show("模板保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 执行
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnExcute_Click(object sender, EventArgs e)
		{
			ReplaceVisualHeaderFile();
		}

		/// <summary>
		/// 选择文件执行
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnSelectFileTemplatePath_Click(object sender, EventArgs e)
		{
			try
			{
				var dialog = new OpenFileDialog();
				dialog.DefaultExt = ".cs";
				dialog.Multiselect = false;
				dialog.Filter = "cs文件(.cs)|*.cs";

				if (dialog.ShowDialog() != DialogResult.OK)
				{
					return;
				}

				string filePath = dialog.FileName;

				//添加到配置中
				AddTemplateConfiguration(filePath);

				//备份原有模板文件
				BackUpTemplateFile(filePath);

				//调用替换方法
				ReplaceVisualHeaderFile(filePath);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 添加目标文件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void simpleButton3_Click(object sender, EventArgs e)
		{
			try
			{
				if (string.IsNullOrEmpty(txtVisualStudioTemplatePath.Text))
				{
					MessageBox.Show("Visual Studio 程序未安装,请先安装！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				var dialog = new OpenFileDialog();
				dialog.DefaultExt = ".cs";
				dialog.Multiselect = false;
				dialog.Filter = "cs文件(.cs)|*.cs";

				if (dialog.ShowDialog() != DialogResult.OK)
				{
					return;
				}

				string filePath = dialog.FileName;

				if (!File.Exists(filePath))
				{
					MessageBox.Show("模板文件不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				string searchString = "IDE\\";
				int index = filePath.IndexOf(searchString);
				if (index == -1)
				{
					MessageBox.Show("请选择Visual Studio安装目录下的模板文件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				int startIndex = index + searchString.Length;
				string path = filePath.Substring(startIndex);

				if (FilePathModels.Any(p => p.FilePath.Equals(path)))
				{
					MessageBox.Show("当前列表中已存在选择的目标文件！！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

			    //读取默认的头文件注释模板
			    string headerFileConfigurationPath = Path.Combine(_LocalDirectory, c_AuxiliaryTools, c_HeaderFileCommentTemplate, c_HeaderFilePathConfiguration);

				//读取头文件配置路径
				FilePathModels.Add(new FilePathModel { FilePath = path });

				using (StreamWriter writer = new StreamWriter(headerFileConfigurationPath))
				{
					writer.Write("");
					foreach (var item in FilePathModels)
					{
						writer.WriteLine(item.FilePath);
					}
				}

				//刷新数据源
				bsData.DataSource = FilePathModels;
				gridControl1.Views[0].RefreshData();

				MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 还原文件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void simpleButton4_Click(object sender, EventArgs e)
		{
			try
			{
				string backupDirectory = Path.Combine(_LocalDirectory, c_AuxiliaryTools, c_HeaderFileCommentTemplate, c_BackupTemplateName);
				if (!Directory.Exists(backupDirectory))
				{
					MessageBox.Show("没有可还原的文件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				DirectoryInfo directoryInfo = new DirectoryInfo(backupDirectory);
				var files = directoryInfo.GetFiles("*.cs").ToList();
				if (files?.Count <= 0)
				{
					MessageBox.Show("没有可还原的文件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				foreach (var file in files)
				{
					if (string.IsNullOrEmpty(file?.FullName) || !File.Exists(file.FullName))
					{
						continue;
					}

					//读取备份文件内容
					string content = File.ReadAllText(file.FullName);
					if (string.IsNullOrEmpty(content))
					{
						continue;
					}

					List<string> paths = file.Name.Split('_').ToList();
					if (paths?.Count == 0)
					{
						continue;
					}

					//构建要还原的文件路径
					string originalFileName = string.Empty;
					int i = 1;
					foreach (string path in paths)
					{
						originalFileName += path + (i < paths.Count ? "\\" : "");
						i++;
					}

					//写入到原文件中
					File.WriteAllText(Path.Combine(txtVisualStudioTemplatePath.Text, originalFileName), content);
				}

				MessageBox.Show("还原文件成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 选择文件路径
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnSelectFilePath_Click(object sender, EventArgs e)
		{
			try
			{
				using (var folderDialog = new FolderBrowserDialog())
				{
					folderDialog.Description = "请选择文件夹";
					folderDialog.ShowNewFolderButton = true; // 允许新建文件夹

					if (folderDialog.ShowDialog() != DialogResult.OK)
					{
						return;
					}

					txtVisualStudioTemplatePath.Text = folderDialog.SelectedPath;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 双击事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void gridControl1_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			var gridView = gridControl1.MainView as GridView;
			if(gridView == null)
			{
				return;
			}

			var selectedRow = gridView.GetFocusedRow() as FilePathModel;
			if(selectedRow == null)
			{
				return;
			}

			try
			{
				string filePath = Path.Combine(txtVisualStudioTemplatePath.Text, selectedRow.FilePath);

				if (!File.Exists(filePath))
				{
					MessageBox.Show("文件不存在或已被删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				Process.Start(new ProcessStartInfo
				{
					FileName = "cmd",
					Arguments = $"/c start \"\" \"{filePath}\"",
					WindowStyle = ProcessWindowStyle.Hidden
				});
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 右键选择文件执行
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnExecuteSelectData_Click(object sender, EventArgs e)
		{
			var gridView = gridControl1.MainView as GridView;
			if (gridView == null)
			{
				return;
			}

			var selectedRow = gridView.GetFocusedRow() as FilePathModel;
			if (selectedRow == null)
			{
				return;
			}
			try
			{
				string filePath = Path.Combine(txtVisualStudioTemplatePath.Text.Trim(), selectedRow.FilePath);
				if (!File.Exists(filePath))
				{
					MessageBox.Show($"{filePath} 文件不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				//备份原有模板文件
				BackUpTemplateFile(filePath);

				//调用替换方法
				ReplaceVisualHeaderFile(filePath);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				var gridView = gridControl1.MainView as GridView;
				if (gridView == null)
				{
					return;
				}

				var selectedRow = gridView.GetFocusedRow() as FilePathModel;
				if (selectedRow == null)
				{
					return;
				}
				if (FilePathModels == null || string.IsNullOrEmpty(selectedRow.FilePath))
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				if (!FilePathModels.Any(p => p.FilePath == selectedRow.FilePath))
				{
					return;
				}

				FilePathModels.Remove(selectedRow);

				//读取默认的头文件注释模板
				string headerFileConfigurationPath = Path.Combine(_LocalDirectory, c_AuxiliaryTools, c_HeaderFileCommentTemplate, c_HeaderFilePathConfiguration);

				//读取头文件配置路径
				using (StreamWriter writer = new StreamWriter(headerFileConfigurationPath))
				{
					writer.Write("");
					foreach (var path in FilePathModels)
					{
						writer.WriteLine(path.FilePath);
					}
				}

				//刷新数据源
				bsData.DataSource = FilePathModels;
				gridControl1.Views[0].RefreshData();

				MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 右键菜单显示事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
		{
			// 检查是否是右键点击
			if (e.HitInfo.InRowCell)
			{
				// 显示您的 ContextMenuStrip
				contextMenuStrip1.Show(Cursor.Position);
			}
		}

		#endregion
	}
}
